import {useContext,useEffect,useMemo} from 'react'
import { useNavigate } from 'react-router-dom'
import context from '../store/context'

export function useUser(){
    const { userInfo,dispatch } = useContext(context)
    return {userInfo,dispatch}
}

export function useLogin(){
    const navigate = useNavigate()
    const {userInfo} = useUser()

    // 判断用户是否登录
    const isLogin = useMemo(() => !!userInfo.authorization, [userInfo])
    console.log('isLogin',isLogin)
    useEffect(()=>{
        if(!isLogin){
            navigate('/login')
        }

    },[isLogin])
}